<?php

class Default_Model_ArticleSet
{

    public $index;
    public $id;
    public $type;
    public $from;
    public $date;
    public $subject;
    public $ref;
    public $items; 
    public $alsoin;


    function __construct($articleNode=NULL)
    {
        if ($articleNode) { 
            $att = $articleNode->attributes();
            $this->items    = array();
            $this->id       = "$articleNode->id";
            $this->index    = $att['index'];
            $this->type     = "$articleNode->type";
            $this->date     = "$articleNode->date";
            $this->subject  = "$articleNode->subject";
            $this->filename = deSubject($this->subject); #"$articleNode->subject";
            if (preg_match('/\.(jpg|jpeg|bmp|png|gif)$/',$this->filename,$arr)) {
                $this->extension = $arr[1];
                $this->type = 'picture';
            }
            $this->ref      = "$articleNode->ref";
            $this->from     = array('name'=>"$articleNode->name",'email'=>"$articleNode->email");
            $this->alsoin   = array();

            $array = explode ('|', (string) $articleNode->ref);
	    for ($fn=0;$fn<sizeof($array);$fn++) {
                if ($array[$fn]!='Xref' && !$this->alsoin[$array[$fn]]) {
                    $this->alsoin[$array[$fn]] = $array[$fn];
                }
            }

            $kids = $articleNode->children();  
            foreach ($kids as $kid) {
 
                if ($kid->getName()=='items') {
                    $items = $kid->children();  
                    foreach ($items as $item) 
                        $this->items[] = new Default_Model_Article($item);

                }
            } 

          //  uasort($this->items, 'cmp');

        }
    } 

    function CheckPreview($groupName) {  

        if (!is_video($this->type)) return; 
        $db = new Usenet_Connector;

        for ($i=0;$i<sizeof($this->items);$i++) { # as $item) {
            $item  = $this->items[$i];
            $array = explode ('-' ,$item->id);
            $Key   = $array[0];
            $sql   = "SELECT mediauri, imageuri FROM MediaIndex WHERE articleid = '{$Key}'";
            $ret   = $db->Execute ($sql);
            if($row = mysql_fetch_assoc($ret)) {

                $item->image = $row['imageuri'];
                $item->media = $row['mediauri'];
                $this->image = $item->image;
                $this->media = $item->media;  
            }
            continue; 

            $idx   = DATA_PATH . "/download/{$groupName}/{$Key}.xml";  

            if (!file_exists($idx)) 
                 continue;

            if (filesize($idx) > 0) {
                $data  = simplexml_load_file($idx); 
                $image = (string) $data[0]->image;
                if (!file_exists($image)) continue;
                $item->image = (string) $data[0]->image;
                $item->media = (string) $data[0]->media; 
                $this->image = $item->image;
                $this->media = $item->media;  
            }

        } 

        return;

        $array = explode ('-' ,$this->id);
        $Key   = $array[0];
        $idx   = DATA_PATH . "/download/{$groupName}/{$Key}.xml"; 
        if (!file_exists($idx)) return;
        if (filesize($idx) > 0) {

            $data  = simplexml_load_file($idx); 
            $this->image = (string) $data[0]->image;
            $this->media = (string) $data[0]->media; 
            return;
        }
       # unlink ($idx);
    } 

    function FindById($id) {
       foreach ($this->items as $item)  
           if (strpos($item->id,$id)!==false)
               return $item; 
    }
}

 



